package cn.com.cifi.mars.service.impl;

import cn.com.cifi.mars.common.enums.LocalCommandEnum;
import cn.com.cifi.mars.common.enums.RedisChannelEnum;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import cn.com.cifi.mars.service.CacheService;

import java.util.Set;

@Service
public class CacheServiceImpl implements CacheService{

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public void removeKey(String key) {
        if (key.contains("*")) {
            Set keys = redisTemplate.keys(key);
            if (CollectionUtils.isNotEmpty(keys)) {
                redisTemplate.delete(keys);
            }
        } else {
            redisTemplate.delete(key);
        }
    }

    @Override
    public void pushCommand(RedisChannelEnum redisChannelEnum, LocalCommandEnum localCommandEnum) {
        redisTemplate.convertAndSend(redisChannelEnum.getCode(),localCommandEnum.getCode());
    }
}
